<template>
  <p-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <p-form layout="inline" @keyup.enter.native="searchQuery">
        <p-row :gutter="24">
          <!-- 条码扫描  -->
          <p-col span="7">
            <p-form-item label="条码扫描">
              <p-input placeholder style="width: 200px" />
            </p-form-item>
          </p-col>
          <!-- 二级工序下拉输入框 -->
          <p-col span="7">
            <p-form-item label="二级工序">
              <j-dict-select-tag
                placeholder="请选择"
                v-model="queryParam.filedType"
                dictCode="mes_filed_type"
              />
            </p-form-item>
          </p-col>
          <!-- 线体下拉输入框 -->
          <p-col span="5">
            <p-form-item label="线体">
              <j-dict-select-tag
                placeholder="请选择"
                v-model="queryParam.filedType"
                dictCode="mes_filed_type"
              />
            </p-form-item>
          </p-col>
          <!-- 工站下拉输入框 -->
          <p-col span="5">
            <p-form-item label="工站">
              <j-dict-select-tag
                placeholder="请选择"
                v-model="queryParam.filedType"
                dictCode="mes_filed_type"
              />
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>

      <!-- 按钮区域 -->
      <div>
        <!-- 产品测试按钮 -->
        <p-button class="BtMargin" @click="handleAdd" type="primary">产品测试</p-button>
        <!-- 超领物料 -->
        <p-button class="BtMargin" @click="handleAdd" type="primary">超领物料</p-button>
        <!-- 生产退料 -->
        <p-button class="BtMargin" @click="handleAdd" type="primary">生产退料</p-button>
        <span class="print">
          是否在线打印
          <p-switch class="switch" checked-children="开" un-checked-children="关" default-checked />
        </span>
      </div>
    </div>
    <!-- 查询区域-END -->

    <!-- 左-上-下布局 -->
    <div style="margin-top:15px;margin-left:10px">
      <p-row :gutter="24">
        <!-- 左侧Table区域 -->
        <p-col :span="8" style="height:500px;border:1px solid #eee;padding:0">
          <p-row style="padding:0 5px" class="ProcessTheRepairOrderTable_header">
            <p-col style="line-height:40px;font-weight:bold;font-family:宋体" span="16">
              <div>工序工单列表</div>
            </p-col>
            <p-col style="line-height:40px" span="4">
              <div>
                <p-checkbox></p-checkbox>全选
              </div>
            </p-col>
            <p-col style="line-height:40px;padding-left:30px" span="4">
              <div>刷新</div>
            </p-col>
          </p-row>
          <p-row style="padding:0 5px" class="putBoxShad">
            <p-col style="line-height:40px;" :span="8">
              <div style="word-break:break-all; width:80px;line-height:20px">500002912</div>
              <div style="word-break:break-all; width:80px;line-height:20px">融合天线V2</div>
            </p-col>
            <p-col style="line-height:40px;" :span="8">
              <div style="word-break:break-all; width:80px;line-height:20px">1000PCS</div>
              <div style="word-break:break-all; width:80px;line-height:20px">2019-03-31</div>
            </p-col>
            <p-col :span="2" style="padding:10px 0">
              <p-checkbox></p-checkbox>
            </p-col>
            <p-col style="line-height:40px;padding-left:30px" :span="6">
              <p-button type="primary">
                <div>开工</div>
              </p-button>
            </p-col>
          </p-row>
        </p-col>
        <!-- 右侧Table区域 -->
        <p-col :span="16" class="ProcessDetails">
          <!-- 右上侧Table区域 -->
          <p-row
            :span="24"
            style="height:240px;border:1px solid #eee;line-height:40px;padding-left:10px"
          >
            <p-col :span="24" style="font-weight:bold;font-family:宋体">工序加工详情</p-col>

            <!-- 工序工单输入框 -->
            <p-col :span="8">
              <p-row :span="24">
                <label for>工序工单：</label>
                <p-input placeholder="请输入" style="width: 150px" />
              </p-row>
            </p-col>
            <!-- SN/PN输入框 -->
            <p-col :span="8">
              <p-row :span="24">
                <label for>SN/PN号：</label>
                <p-input placeholder="请输入" style="width: 150px" />
              </p-row>
            </p-col>
            <!-- 计划数量输入框 -->
            <p-col :span="8">
              <p-row :span="24">
                <label for>计划数量：</label>
                <p-input placeholder="1000" style="width: 150px" />
              </p-row>
            </p-col>
            <div style="padding-left:40px">
              <!-- 加工中区域 -->
              <p-col :span="8" style="margin-rigth:20px">
                <p-row :span="24">
                  <div
                    style="background:blue;border:1px solid #0066FF;height:120px;width:200px;margin-top:20px;border-radius:5px;color:#fff;display:flex;justify-content:center;align-items:center;flex-direction:column"
                  >
                    <div style="font-size:30px;font-weight:500">加工中</div>
                    <div style="font-size:30px;font-weight:500">0</div>
                  </div>
                </p-row>
              </p-col>
              <!-- 合格数量区域 -->
              <p-col :span="8" style="margin-rigth:20px">
                <p-row :span="24">
                  <div
                    style="background:#999999;border:1px solid #0066FF;height:120px;width:200px;margin-top:20px;border-radius:5px;color:#fff;display:flex;justify-content:center;align-items:center;flex-direction:column"
                  >
                    <div style="font-size:30px;font-weight:500">合格数量</div>
                    <div style="font-size:30px;font-weight:500">0</div>
                  </div>
                </p-row>
              </p-col>
              <!-- 不合格数量区域 -->
              <p-col :span="8">
                <p-row :span="24">
                  <div
                    style="background:#FF0000;border:1px solid #0066FF;height:120px;width:200px;margin-top:20px;border-radius:5px;color:#fff;display:flex;justify-content:center;align-items:center;flex-direction:column"
                  >
                    <div style="font-size:30px;font-weight:500">不合格数量</div>
                    <div style="font-size:30px;font-weight:500">0</div>
                  </div>
                </p-row>
              </p-col>
            </div>
          </p-row>
          <!-- 右下侧Table区域 -->
          <p-row :span="24" style="height:240px;border:1px solid #eee;margin-top:20px">
            <p-col :span="12" style="line-height:40px;padding-left:15px">排班人员信息</p-col>
            <p-col :span="12" style="line-height:40px">
              <p-select
                style="width:200px;float:right;margin:10px 30px 10px 0"
                placeholder="排班信息"
                v-model="queryParam.filedType"
                dictCode="mes_filed_type"
              />
            </p-col>
            <p-col :span="24">
              <p-table
                :columns="columns"
                :data-source="data"
                :pagination="{ pageSize: 50 }"
                :scroll="{ y: 240 }"
                bordered
              />
            </p-col>
          </p-row>
        </p-col>
      </p-row>
    </div>
  </p-card>
</template>

<script>
//1.引入JDictSelectTag组件
import JDictSelectTag from "@/components/dict/JDictSelectTag.vue";

import { JeecgListMixin } from "@/mixins/JeecgListMixin";
import { api } from "@/api/manage";
import JSuperQuery from "@/components/jeecg/JSuperQuery.vue";
import { filterObj } from "@/utils/util";
import MesMfProcessModal from "./modules/MesMfProcessModal";

import Vue from "vue";
import {
  initDictOptions,
  filterMultiDictText,
} from "@/components/dict/JDictSelectUtil";
import ButtonExpHandler from "@/components/jeecgbiz/modules/ButtonExpHandler";
import { postAction, getAction, deleteAction } from "@/api/manage";
import FxCompontensModal from "@/components/jeecgbiz/FxCompontensModal";

//高级查询modal需要参数
const superQueryFieldList = [
  {
    value: "productType",
    text: "产品类型",
    type: "string",
  },
  {
    value: "productCategory",
    text: "产品类别",
    type: "string",
  },
  {
    value: "processType",
    text: "工单类型",
    type: "select",
    dictCode: "mes_process_type",
  },
  {
    value: "taskSource",
    text: "任务来源",
    type: "string",
  },
  {
    value: "selfControl",
    text: "自制/外协",
    type: "string",
  },
  {
    value: "productionno",
    text: "工单号",
    type: "string",
  },
  {
    value: "processbillno",
    text: "工序工单号",
    type: "string",
  },
  {
    value: "processTwocode",
    text: "二级工序编码",
    type: "string",
  },
  {
    value: "processTwoname",
    text: "二级工序名称",
    type: "string",
  },
  {
    value: "lineno",
    text: "线体编码",
    type: "string",
  },
  {
    value: "linename",
    text: "线体名称",
    type: "string",
  },
  {
    value: "mcode",
    text: "物料编码",
    type: "String",
  },
  {
    value: "mname",
    text: "物料名称",
    type: "String",
  },
  {
    value: "bomversions",
    text: "BOM版本",
    type: "string",
  },
  {
    value: "billnoStatus",
    text: "工单状态",
    type: "select",
    dictCode: "mes_billno_status",
  },
  {
    value: "starttime",
    text: "计划开始时间",
    type: "Date",
  },
  {
    value: "completiontime",
    text: "计划完成时间",
    type: "Date",
  },
  {
    value: "qistate",
    text: "物料齐套状态",
    type: "select",
    dictCode: "mes_qistate",
  },
  {
    value: "planQty",
    text: "计划数量",
    type: "string",
  },
  {
    value: "schedule",
    text: "完成进度",
    type: "string",
  },
  {
    value: "bomstatus",
    text: "BOM状态",
    type: "int",
  },
  {
    value: "createBy",
    text: "创建人",
    type: "string",
  },
  {
    value: "createTime",
    text: "创建日期",
    type: "Date",
  },
  {
    value: "updateBy",
    text: "更新人",
    type: "string",
  },
  {
    value: "updateTime",
    text: "更新日期",
    type: "Date",
  },
  {
    value: "remark",
    text: "备注",
    type: "string",
  },
];

export default {
  name: "MesMfProcessList",
  mixins: [JeecgListMixin],
  components: {
    JSuperQuery,
    FxCompontensModal,
    MesMfProcessModal,
    JDictSelectTag,
  },
  data() {
    return {
      fieldList: superQueryFieldList,
      superQueryFlag: false,
      superQueryParams: "",
      description: "工序工单计划管理页面",
      cgButtonLinkList: [],
      title: "", //存表名
      EnhanceJS: "",
      cgButtonList: [],
      groupTag: [],
      queryInfo: [],
      code: "",
      // 表头
      columns: [],
      //列设置
      settingColumns: [],
      //列定义
      defColumns: [
        {
          title: "序号",
          dataIndex: "",
          key: "rowIndex",
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          },
        },
        {
          title: "线体",
          align: "center",
          dataIndex: "processType_dictText",
        },
        {
          title: "工站",
          align: "center",
          dataIndex: "taskSource",
        },
        {
          title: "排班人员",
          align: "center",
          dataIndex: "processbillno",
        },
        {
          title: "上岗人员",
          align: "center",
          dataIndex: "processTwocode",
        },
        {
          title: "匹配结果",
          align: "center",
          dataIndex: "processTwoname",
        },
        {
          title: "上岗时间",
          align: "center",
          dataIndex: "lineno",
        },
        {
          title: "迁出时间",
          align: "center",
          dataIndex: "linename",
        },
        {
          title: "操作",
          align: "center",
          dataIndex: "mcode",
        },
      ],
      url: {
        list: "/mes-manufacture/auto/mesMfProcess/list",
        delete: "/mes-manufacture/auto/mesMfProcess/delete",
        deleteBatch: "/mes-manufacture/auto/mesMfProcess/deleteBatch",
        exportXlsUrl: "/mes-manufacture/auto/mesMfProcess/exportXls",
        importExcelUrl: "/mes-manufacture/auto/mesMfProcess/importExcel",
        getColumns: "/mes-manufacture/online/cgform/api/getColumns/",
        buttonAction: "/mes-manufacture/online/cgform/api/doButton",
      },
      dictOptions: {},
    };
  },
  computed: {
    importExcelUrl: function () {
      return `${api.test}${this.url.importExcelUrl}`;
    },
    getWidth: function () {
      if (this.defColumns.length > 14) {
        return { width: "500px" };
      }
    },
    tableScroll: function () {
      if (this.columns.length > 13) {
        return { x: this.columns.length * 150 - 450 };
      }
    },
  },
  created() {
    this.initButtonList();
    this.initColumns();
    this.setdefColumns();
  },

  methods: {
    setdefColumns() {
      if (this.columns.length > 13) {
        let arrLength = this.columns.length - 1;
        this.columns.forEach((item, index) => {
          item["width"] = 130;
        });
        this.columns[arrLength]["fixed"] = "right";
        this.columns[0]["fixed"] = "left";
        this.columns[1]["fixed"] = "left";
      }
    },
    initColumns() {
      //权限过滤（列权限控制时打开，修改第二个参数为授权码前缀）
      //this.defColumns = colAuthFilter(this.defColumns,'testdemo:');

      var key = this.$route.name + ":colsettings";
      let colSettings = Vue.ls.get(key);
      if (colSettings == null || colSettings == undefined) {
        let allSettingColumns = [];
        this.defColumns.forEach(function (item, i, array) {
          allSettingColumns.push(item.dataIndex);
        });
        this.settingColumns = allSettingColumns;
        this.columns = this.defColumns;
      } else {
        this.settingColumns = colSettings;
        const cols = this.defColumns.filter((item) => {
          if (item.key == "rowIndex" || item.dataIndex == "action") {
            return true;
          }
          if (colSettings.includes(item.dataIndex)) {
            return true;
          }
          return false;
        });
        this.columns = cols;
      }
    },
    //列设置更改事件
    onColSettingsChange(checkedValues) {
      var key = this.$route.name + ":colsettings";
      Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000);
      this.settingColumns = checkedValues;
      const cols = this.defColumns.filter((item) => {
        if (item.key == "rowIndex" || item.dataIndex == "action") {
          return true;
        }
        if (this.settingColumns.includes(item.dataIndex)) {
          return true;
        }
        return false;
      });
      this.columns = cols;
    },

    //高级查询方法
    handleSuperQuery(arg) {
      if (!arg) {
        this.superQueryParams = "";
        this.superQueryFlag = false;
      } else {
        this.superQueryFlag = true;
        this.superQueryParams = JSON.stringify(arg);
      }
      this.loadData();
    },
    // 获取查询条件
    getQueryParams() {
      let sqp = {};
      if (this.superQueryParams) {
        sqp["superQueryParams"] = encodeURI(this.superQueryParams);
      }
      var param = Object.assign(sqp, this.queryParam, this.isorter);
      param.field = this.getQueryField();
      param.pageNo = this.ipagination.current;
      param.limit = this.ipagination.pageSize;
      return filterObj(param);
    },
    initDictConfig() {},

    // 弹出窗组件
    showDemoModal() {
      this.$refs.modalCompon.modalShow();
    },
    // 点击btn获取数据
    btnGetData(tableName, title) {
      getAction(`${this.url.getColumns}`, {
        tableName: tableName,
      }).then((res) => {
        if (res.code == 0) {
          this.queryInfo = res.data.columns;
          this.title = title;
          this.dictOptions = res.data.dictOptions;
          this.showDemoModal();
        } else {
          this.$message.warning(res.msg);
        }
      });
    },

    //筛选分组
    getGroupTag(arrList) {
      let arr = [];
      var map = {},
        dest = [];
      arrList.forEach((item, index) => {
        if (item.groupTag) {
          arr.push(item);
        }
      });
      for (var i = 0; i < arr.length; i++) {
        var ai = arr[i];
        if (!map[ai.groupTag]) {
          dest.push({
            groupTag: ai.groupTag,
            name: ai.groupName,
            data: [ai],
          });
          map[ai.groupTag] = ai;
        } else {
          for (var j = 0; j < dest.length; j++) {
            var dj = dest[j];
            if (dj.groupTag == ai.groupTag) {
              dj.data.push(ai);
              break;
            }
          }
        }
      }
      this.groupTag = dest;
      console.log(dest);
    },
    showLinkButton(item, record) {
      let btn = new ButtonExpHandler(item.exp, record);
      return btn.show;
    },
    initButtonList() {
      getAction(`${this.url.getColumns}8ac49ec7733bce48017355583cbc011c`).then(
        (res) => {
          console.log("--获取自定义按钮->>", res);
          if (res.success) {
            this.code = res.data.code;
            this.initCgButtonList(res.data.cgButtonList);
            this.initCgEnhanceJs(res.data.enhanceJs);
            this.getGroupTag(res.data.cgButtonList);
          } else {
            this.$message.warning(res.msg);
          }
        }
      );
    },
    initCgEnhanceJs(enhanceJs) {
      //console.log("--onlineList-js增强",enhanceJs)
      if (enhanceJs) {
        let Obj = eval("(" + enhanceJs + ")");
        this.EnhanceJS = new Obj(getAction, postAction, deleteAction);
        this.cgButtonJsHandler("created");
      } else {
        this.EnhanceJS = "";
      }
    },
    initCgButtonList(btnList) {
      let linkArr = [];
      let buttonArr = [];
      if (btnList && btnList.length > 0) {
        for (let i = 0; i < btnList.length; i++) {
          let temp = btnList[i];
          if (temp.buttonStyle == "button") {
            buttonArr.push(temp);
          } else if (temp.buttonStyle == "link") {
            linkArr.push(temp);
          }
        }
      }
      this.cgButtonLinkList = [...linkArr];
      this.cgButtonList = [...buttonArr];
      console.log("链接增强", this.cgButtonLinkList);
    },
    cgButtonJsHandler(buttonCode) {
      if (this.EnhanceJS[buttonCode]) {
        this.EnhanceJS[buttonCode](this);
      }
    },
    cgButtonActionHandler(buttonCode) {
      //处理自定义button的 需要配置该button自定义sql
      if (!this.selectedRowKeys || this.selectedRowKeys.length == 0) {
        this.$message.warning("请先选中一条记录");
        return false;
      }
      if (this.selectedRowKeys.length > 1) {
        this.$message.warning("请只选中一条记录");
        return false;
      }
      let params = {
        formId: this.code,
        buttonCode: buttonCode,
        dataId: this.selectedRowKeys[0],
      };
      console.log("自定义按钮请求后台参数：", params);
      postAction(this.url.buttonAction, params).then((res) => {
        if (res.success) {
          this.loadData();
          this.$message.success("处理完成!");
        } else {
          this.$message.warning("处理失败!");
        }
      });
    },
    cgButtonLinkHandler(record, buttonCode, optType) {
      if (optType == "js") {
        if (this.EnhanceJS[buttonCode]) {
          this.EnhanceJS[buttonCode](this, record);
        }
      } else if (optType == "action") {
        let params = {
          formId: this.code,
          buttonCode: buttonCode,
          dataId: record.id,
        };
        console.log("自定义按钮link请求后台参数：", params);
        postAction(this.url.buttonAction, params).then((res) => {
          if (res.success) {
            this.loadData();
            this.$message.success("处理完成!");
          } else {
            this.$message.warning("处理失败!");
          }
        });
      }
    },
  },
};
</script>
<style scoped>
@import "~@assets/less/common.less";

.BtMargin {
  margin-right: 10px;
}
.sprint i {
  margin-left: 20px;
}

.switch {
  margin-left: 10px;
}
.putBoxShad:hover {
  box-shadow: -1px 0px 20px #333333;
  margin-bottom: 10px;
}
.ant-empty-normal {
  margin: 0;
}
</style>
